const config = require('../../config')
const env = require('../../env')
const util = require('../../utils/util')
const restService = require('../../services/restService')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    javaQuestions: [],
    javaQuestionList: [],
    isLoading: true,
    imageUrl: config.url.images,
    moduleCode: '',
    title: '',
    pageIndex: 1,
    pageSize: 10,
    total: 0
  },

  onLoad(e) {
    console.log(e)
    this.setData({
      moduleCode: e.moduleCode,
      title: e.title
    })
    wx.setNavigationBarTitle({
      title: e.title,
    })
  },
  
  onShow: function () {
    var pageIndex = this.options.pageIndex
    var pageSize = this.options.pageSize
    this.getJavaQuestionList(this.options.moduleCode,pageIndex,pageSize)
  },

  onShareAppMessage() {
    // return custom share data when user share.
  },

  getJavaQuestionList: function (moduleCode,pageIndex,pageSize) {
    restService.commonRequest({
      url: config.service.getJavaQuestionList,
      data: {
        moduleCode: moduleCode,
        pageIndex: pageIndex,
        pageSize: pageSize
      },
      method: 'POST',
      success: res => {
        const respData = res.data;
        const javaQuestions = respData.records;
        this.setData({
          javaQuestions: javaQuestions,
          javaQuestionList: javaQuestions,
          isLoading: false,
          pageIndex: respData.current,
          total: respData.total
        });
      },
      fail: err => {
        console.log(err, 'error')
      }
    })
  },
  
  formatList: function (javaQuestions, type) {
    let javaQuestionList = []
    for (let javaQuestion of javaQuestions) {
      javaQuestionList.push({
        id: javaQuestion._id,
        imageUrl: this.getImageUrl(type),
        answerUrl: javaQuestion.answer,
        title: javaQuestion.question,
        subTitle: javaQuestion.subTitle ? javaQuestion.subTitle : ''
      })
    }
    return javaQuestionList
  },

  getImageUrl: function(type) {
    let imageUrl = '';
    switch(type) {
      case "javaBasic" : 
        imageUrl = "../../images/index/index_answer.png"
        break
      case "javaBasic" : 
        imageUrl = "../../images/index/icon_code.png"
        break
      default:
        imageUrl = "../../images/index/index_answer.png"
        break
    }
    return imageUrl;
  },
  /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
      console.log("查询后台")
      var pageIndex = this.data.pageIndex;
      var pageSize = this.data.pageSize;
      var total = this.data.total
      var currentTotal = this.data.javaQuestionList.length
      if (total != currentTotal){
        console.log("实际查询")
        restService.commonRequest({
          url: config.service.getJavaQuestionList,
          data: {
            moduleCode: this.options.moduleCode,
            pageIndex: pageIndex+1,
            pageSize: pageSize
          },
          method: 'POST',
          success: res => {
            const respData = res.data;
            const javaQuestions = respData.records;
            this.setData({
              javaQuestions: this.data.javaQuestions.concat(javaQuestions),
              javaQuestionList: this.data.javaQuestionList.concat(javaQuestions),
              isLoading: false,
              pageIndex: respData.current,
              total: respData.total
            });
          },
          fail: err => {
            console.log(err, 'error')
          }
        })
      } else {
        util.showToTopTip("没有更多数据了")
      }
    },
})